package com.macrounion.atv.listener;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.macrounion.atv.cache.ChannelCache;
import com.macrounion.atv.dto.req.SnmpGetRouterReqDto;
import com.macrounion.atv.processor.ChannelProcessor;
import com.macrounion.atv.processor.CurrentDataProcessor;
import com.macrounion.atv.service.entity.Channel;
import com.macrounion.atv.service.entity.CurrentData;
import com.macrounion.atv.service.entity.ProtocolC;
import com.macrounion.atv.service.entity.ext.DeviceExt;
import com.macrounion.device.listener.ObjectReceiveEvent;
import com.macrounion.device.resolver.dto.SnmpDeviceAttrDto;

@Component
public class SnmpGetDeviceDataListener implements com.macrounion.device.listener.ObjectReceiveListener<SnmpDeviceAttrDto>{

	@Autowired
	private CurrentDataProcessor currentDataProcessor;
	@Autowired
	private ChannelCache channelCache;
	@Autowired
	private ChannelProcessor channelProcessor;
	
	@Override
	public void receive(ObjectReceiveEvent<SnmpDeviceAttrDto> event) {
		SnmpGetRouterReqDto deviceDto = (SnmpGetRouterReqDto) event.getSource();
//		System.out.println(deviceDto.toString());
		SnmpDeviceAttrDto dto = event.getObject();
		//格式化数据
		List<CurrentData> datas = convertDataFromSnmpGet(deviceDto,dto);
		//存储数据
		if(deviceDto!=null && deviceDto.getDevice()!=null&&!StringUtils.isEmpty(deviceDto.getDevice().getId())) {
			currentDataProcessor.saveDataFromSnmp(deviceDto.getDevice().getId(),datas,"curdata");
			//存储虚拟通道
			datas.forEach(d->{
				Channel c = channelCache.getChannel(d.getDeviceId(), d.getChannelCode());
				if(c!=null && StringUtils.isEmpty(c.getVirtualId())) {
					channelProcessor.saveVirtual(deviceDto.getDevice(), c);
				}
			});
		}
			
	}

	private List<CurrentData> convertDataFromSnmpGet(SnmpGetRouterReqDto deviceDto, SnmpDeviceAttrDto datas) {
		List<CurrentData> resp = Lists.newArrayList();
		Map<String,ProtocolC> protocolcMap = deviceDto.getProtocolcs().parallelStream().collect(Collectors.toMap(ProtocolC::getMid, o->o));
		datas.getDetails().forEach(d->{
			CurrentData data = new CurrentData();
			ProtocolC pc = protocolcMap.get(d.getOid());
			DeviceExt device = deviceDto.getDevice();
			if(pc != null & device!=null) {
				data.setChannelCode(pc.getChannelCode());
				Channel c = channelCache.getChannel(device.getId(), pc.getChannelCode());
				if(c!=null) {
					data.setId(UUID.randomUUID().toString());
					data.setChannelId(c.getId());
					data.setCreatedTime(new Date());
					data.setDeviceCode(device.getDeviceCode());
					data.setDeviceId(device.getId());
					data.setShowValue(channelProcessor.getVaule(c,d.getValue()));
					data.setSpotId(device.getSpotId());
					data.setUnit(c.getUnit());
					data.setValues(d.getValue());
					Integer ala = channelProcessor.getAlarm(c,data.getShowValue());
					data.setAlarm(ala);
					channelProcessor.sendAlarmToItu(device,c,data);
					resp.add(data);
				}
			}
		});
		return resp;
	}

}
